package com.ym.script.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Map.Entry;

public class HttpUtils {

	public static String get(String url, Map<String, String> args)
			throws IOException {
		StringBuilder fullHostUrl = new StringBuilder(url).append("?");

		if (args != null) {
			for (Entry<String, String> entry : args.entrySet()) {
				String key = entry.getKey();
				String value = entry.getValue();
				fullHostUrl
						.append(key)
						.append("=")
						.append(URLEncoder.encode(String.valueOf(value),
								"UTF-8"));

				fullHostUrl.append("&");
			}
		}
		URL host = new URL(fullHostUrl.toString());
		HttpURLConnection connection = HttpURLConnection.class.cast(host
				.openConnection());
		connection.setConnectTimeout(6000);
		connection.setReadTimeout(6000);

		connection.setRequestMethod("GET");
		connection.setUseCaches(false);
		connection.setRequestProperty("Connection", "Keep-Alive");
		connection.setRequestProperty("Charsert", "UTF-8");
		int resultCode = connection.getResponseCode();
		StringBuilder response = new StringBuilder();
		if (resultCode == HttpURLConnection.HTTP_OK) {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {
				response.append(line);
				response.append("\n");
			}
			br.close();
		} else {
			response.append(resultCode);
		}
		return response.toString();
	}

	public static String post(String url, byte[] params) throws IOException {
		URL host = new URL(url);
		HttpURLConnection connection = HttpURLConnection.class.cast(host
				.openConnection());
		connection.setConnectTimeout(3000);
		connection.setReadTimeout(3000);
		connection.setRequestMethod("POST");
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty("Connection", "Keep-Alive");
		connection.setRequestProperty("Charsert", "UTF-8");
		DataOutputStream dos = new DataOutputStream(
				connection.getOutputStream());
		if (params != null) {
			dos.write(params);
		}
		dos.flush();
		dos.close();

		int resultCode = connection.getResponseCode();
		StringBuilder response = new StringBuilder();
		if (resultCode == HttpURLConnection.HTTP_OK) {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {
				response.append(line);
			}
			br.close();
		} else {
			response.append(resultCode);
		}
		return response.toString();
	}

	public static String post(String url, String params) throws IOException {

		return post(url, params.getBytes("UTF-8"));
	}

	public static String post(String url, Map<String, String> params)
			throws IOException {
		URL host = new URL(url);
		HttpURLConnection connection = HttpURLConnection.class.cast(host
				.openConnection());
		connection.setConnectTimeout(3000);
		connection.setReadTimeout(3000);
		connection.setRequestMethod("POST");
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty("Connection", "Keep-Alive");
		connection.setRequestProperty("Charsert", "UTF-8");
		DataOutputStream dos = new DataOutputStream(
				connection.getOutputStream());
		if (params != null) {
			for (Entry<String, String> entry : params.entrySet()) {
				String key = entry.getKey();
				Object value = entry.getValue();

				dos.write((key + "=" + URLEncoder.encode(String.valueOf(value),
						"UTF-8")).getBytes());
				dos.write("&".getBytes());
			}
		}
		dos.flush();
		dos.close();

		int resultCode = connection.getResponseCode();
		StringBuilder response = new StringBuilder();
		if (resultCode == HttpURLConnection.HTTP_OK) {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {
				response.append(line);
			}
			br.close();
		} else {
			response.append(resultCode);
		}
		return response.toString();
	}

}
